package com.data.web.controller.sensor.point;

import com.data.biz.domain.WindPoint;
import com.data.biz.service.IBizSensorCommonService;
import com.data.biz.vo.SensorVo.point.WindPointVo;
import com.data.common.annotation.Log;
import com.data.common.constant.UserConstants;
import com.data.common.core.controller.BaseController;
import com.data.common.core.domain.AjaxResult;
import com.data.common.core.page.TableDataInfo;
import com.data.common.enums.BusinessType;
import com.data.common.utils.poi.ExcelUtil;
import com.data.framework.util.ShiroUtils;
import com.data.system.domain.SysPost;
import com.data.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *  功率统计
 *
 * @author xzp
 */
@Controller
@RequestMapping("/sensor/point")
public class PointViewController extends BaseController {
	private String prefix = "sensor/point";

	@Autowired
	private IBizSensorCommonService commonService;

	@RequiresPermissions("sensor:point:view")
	@GetMapping()
	public String operlog()
	{
		return prefix + "/point";
	}

	@RequiresPermissions("sensor:point:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WindPoint point)
	{
		startPage();
		List<WindPointVo> list = commonService.getWindPointlist(point);
		return getDataTable(list);
	}

	@Log(title = "测点管理", businessType = BusinessType.EXPORT)
	@RequiresPermissions("sensor:point:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(WindPoint point)
	{
		List<WindPointVo> list = commonService.getWindPointlist(point);
		ExcelUtil<WindPointVo> util = new ExcelUtil<WindPointVo>(WindPointVo.class);
		return util.exportExcel(list, "测点数据");
	}

	@RequiresPermissions("sensor:point:remove")
	@Log(title = "测点管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		try
		{
			return toAjax(commonService.deletePointByIds(ids));
		}
		catch (Exception e)
		{
			return error(e.getMessage());
		}
	}

	/**
	 * 新增测点
	 */
	@GetMapping("/add")
	public String add()
	{
		return prefix + "/add";
	}

	/**
	 * 新增保存测点
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@Validated WindPoint point) throws Exception {

		if(point.getPointName() == null){
			return error("测点名称不能为空");
		}
//		if (UserConstants.POINT_NAME_NOT_UNIQUE.equals(commonService.checkPointNameUnique(point)))
//		{
//			return error("新增测点'" + point.getPointName() + "'失败，测点名称已存在");
//		}
		return toAjax(commonService.insertPoint(point));
	}

	/**
	 * 修改测点
	 */
	@GetMapping("/edit/{pointId}")
	public String edit(@PathVariable("pointId") Integer pointId, ModelMap mmap)
	{
		mmap.put("point", commonService.selectPointById(pointId));
		return prefix + "/edit";
	}


	/**
	 * 修改保存测点
	 */
	@RequiresPermissions("sensor:point:edit")
	@Log(title = "测点管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@Validated WindPoint point)
	{
		if(point.getPointName() == null){
			return error("测点名称不能为空");
		}
//		if (UserConstants.POINT_NAME_NOT_UNIQUE.equals(commonService.checkPointNameUnique(point)))
//		{
//			return error("修改测点'" + point.getPointName() + "'失败，测点名称已存在");
//		}
		return toAjax(commonService.updatePoint(point));
	}


	/**
	 * 校验测点编码
	 */
	@PostMapping("/checkPointNameUnique")
	@ResponseBody
	public String checkPointNameUnique(WindPoint point)
	{
		return commonService.checkPointNameUnique(point);
	}


} 
